using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class forums_controls_registration_registerform : System.Web.UI.UserControl
{
    public event EventHandler UserCreated;

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "FSTDT Forums: " + "Register New Account";
        if (Managers.UserLoginManager.CurrentUser != null)
        {
            mviewRegister.SetActiveView(viewError);
        }
    }

    protected void valUsernameUnique_ServerValidate(object source, ServerValidateEventArgs args)
    {
        bool usernameAvailable = (DAL.Users.GetByUsername(txtUsername.Text.Trim()) == null);
        valUsernameUnique.ErrorMessage = String.Format("Someone has already registered username '{0}'",
                                            Server.HtmlEncode(txtUsername.Text));
        args.IsValid = usernameAvailable;
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            DAL.User myUser = new DAL.User();
            myUser.Username = txtUsername.Text.Trim();
            myUser.Status = DAL.User.UserStatus.Member;
            DAL.Users.Add(myUser, txtPassword.Text);

            mviewRegister.SetActiveView(viewAuthenticated);

            if (UserCreated != null)
            {
                UserCreated(this, new EventArgs());
            }
        }
    }
}
